package com.fjh.springboot_01.controller;

import com.fjh.springboot_01.pojo.Banner;
import com.fjh.springboot_01.pojo.Result;
import com.fjh.springboot_01.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/banner")
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE})
public class BannerController {
    
    @Autowired
    private BannerService bannerService;
    
    @GetMapping("/active")
    public Result<List<Banner>> getActiveBanners() {
        return Result.success(bannerService.getActiveBanners());
    }
    
    @GetMapping("/list")
    public Result<List<Banner>> getAllBanners() {
        return Result.success(bannerService.getAllBanners());
    }
    
    @PostMapping("/add")
    public Result<Banner> addBanner(@RequestParam("title") String title,
                                  @RequestParam(value = "link", required = false) String link,
                                  @RequestParam("sort") Integer sort,
                                  @RequestParam("status") Integer status,
                                  @RequestParam(value = "imageFile", required = false) MultipartFile imageFile) {
        System.out.println("添加轮播图 - 参数信息：");
        System.out.println("标题: " + title);
        System.out.println("链接: " + link);
        System.out.println("排序: " + sort);
        System.out.println("状态: " + status);
        System.out.println("图片文件: " + (imageFile != null ? imageFile.getOriginalFilename() : "无"));
        
        Banner banner = new Banner();
        banner.setTitle(title);
        banner.setLink(link != null ? link : "");
        banner.setSort(sort);
        banner.setStatus(status);
        return Result.success(bannerService.addBanner(banner, imageFile));
    }
    
    @PutMapping("/update")
    public Result<Banner> updateBanner(@RequestParam("id") Integer id,
                                     @RequestParam("title") String title,
                                     @RequestParam(value = "link", required = false) String link,
                                     @RequestParam("sort") Integer sort,
                                     @RequestParam("status") Integer status,
                                     @RequestParam(value = "imageFile", required = false) MultipartFile imageFile) {
        System.out.println("更新轮播图 - 参数信息：");
        System.out.println("ID: " + id);
        System.out.println("标题: " + title);
        System.out.println("链接: " + link);
        System.out.println("排序: " + sort);
        System.out.println("状态: " + status);
        System.out.println("图片文件: " + (imageFile != null ? imageFile.getOriginalFilename() : "无"));
        
        Banner banner = new Banner();
        banner.setId(id);
        banner.setTitle(title);
        banner.setLink(link != null ? link : "");
        banner.setSort(sort);
        banner.setStatus(status);
        return Result.success(bannerService.updateBanner(banner, imageFile));
    }
    
    @DeleteMapping("/delete/{id}")
    public Result<Void> deleteBanner(@PathVariable Integer id) {
        bannerService.deleteBanner(id);
        return Result.success();
    }
    
    @GetMapping("/detail/{id}")
    public Result<Banner> getBannerById(@PathVariable Integer id) {
        return Result.success(bannerService.getBannerById(id));
    }
} 